/**
 * Created by Jimmy on 15/9/22.
 */
define([
        'app',
        './API',
        './UserAPI'
    ],
    function (app) {
        'use strict';
        app.filter('cost', ['$filter', function ($filter) {
            return function (money) {
                return $filter('currency')(money / 100, '¥');
            };
        }]);
        app.filter('material', ['$filter', function($filter) {
            return function(product) {
                // if (product.product && product.product.url === 'my-bracelet') {
                //     switch (product.options.materialName) {
                //         case 'silver':
                //             return '925银';
                //
                //         case 'gold18Red':
                //             return '9K玫瑰金';
                //
                //         case 'gold18Yellow':
                //             return '9K黄金';
                //
                //         case 'gold18White':
                //             return '9K白金';
                //
                //         case 'gold18WhiteRed':
                //             return '9K玫瑰金&白金';
                //
                //         case 'gold18WhiteYellow':
                //             return '9K黄金&白金';
                //         default:
                //             break;
                //     }
                // }
                // if (product.url && product.url === 'my-bracelet') {
                //     switch (product.options.materialName) {
                //         case 'silver':
                //             return '925银';
                //
                //         case 'gold18Red':
                //             return '9K玫瑰金';
                //
                //         case 'gold18Yellow':
                //             return '9K黄金';
                //
                //         case 'gold18White':
                //             return '9K白金';
                //
                //         case 'gold18WhiteRed':
                //             return '9K玫瑰金&白金';
                //
                //         case 'gold18WhiteYellow':
                //             return '9K黄金&白金';
                //         default:
                //             break;
                //     }
                // }
                switch (product.options.materialName) {
                    case 'silver':
                        return '925银';

                    case 'gold18Red':
                        return '18K玫瑰金';

                    case 'gold18Yellow':
                        return '18K黄金';

                    case 'gold18White':
                        return '18K白金';

                    case 'gold18WhiteRed':
                        return '18K玫瑰金&白金';

                    case 'gold18WhiteYellow':
                        return '18K黄金&白金';
    
                    case 'gold9Red':
                        return '9K玫瑰金';
    
                    case 'gold9Yellow':
                        return '9K黄金';
    
                    case 'gold9White':
                        return '9K白金';
    
                    case 'gold9WhiteRed':
                        return '9K玫瑰金&白金';
    
                    case 'gold9WhiteYellow':
                        return '9K黄金&白金';
                    
                    default:
                        break;
                }

            };
        }]);
        app.factory('ProductAPI', ['API', '$resource', 'UserAPI',
            function (API, $resource, UserAPI) {
                var _API = new API('product');
                var server = _API.server;
                _API.material = angular.extend(new API('product/material'), {});
                _API.category = angular.extend(new API('product/category'), {});
                _API.imageType = angular.extend(new API('product/image-type'), {});
                _API.image = angular.extend(new API('product/image'), {});
                _API.model = angular.extend(new API('product/model'), {});
                _API.discount = angular.extend(new API('product/discount'), {});
                _API.price = angular.extend(new API('product/price'), {});
                _API.tag = angular.extend(new API('product/tag'), {});
                _API.template = angular.extend(new API('product/template'), {});
                _API.salesProperty = angular.extend(new API('product/sales-property'), {});
                _API.series = angular.extend(new API('product/series'), {});

                var _dataList = $resource(server + '/data/list', null, {
                    'get': {
                        cache: true
                    }
                });
                var _dataUrl = $resource(server + '/data/:url', null, {
                    'get': {
                        cache: true
                    }
                });
                return angular.extend(_API, {
                    dataList: function (index, total, options) {
                        index = index || 0;
                        total = total || 10;
                        options = options || {};
                        options.fields = options.fields || [];
                        var has = false;
                        for (var i = 0; i < options.fields.length; i++) {
                            if (options.fields[i].key === 'status' && options.fields[i].actions === "greaterThanOrEqualTo") {
                                has = true;
                                break;
                            }
                        }
                        if (!has) {
                            //3是用户,不登录默认3
                            //TODO  改为3
                            var status = 3;
                            var curentRole = UserAPI.currentRoles() || [];
                            if (curentRole.indexOf('Engineer') !== -1) {
                                //技术部内测
                                status = 1;
                            } else if (curentRole.indexOf('Employee') !== -1) {
                                //马良行内测
                                status = 2;
                            }
                            options.fields.push({
                                action: 'greaterThanOrEqualTo',
                                key: 'status',
                                value: status
                            });
                        }

                        options.sorts = options.sorts || [];
                        return _dataList.get({
                            index: index,
                            total: total,
                            fields: new Array(options.fields),
                            sorts: new Array(options.sorts)
                        }).$promise;
                    },
                    dataUrl: function (url) {
                        return _dataUrl.get({
                            url: url
                        }).$promise;
                    }
                });
            }
        ]);
    });
